package com.lancoo.znbkxx.teacher.ui.activity

import android.content.Intent
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.base.BaseVMActivity
import com.lancoo.znbkxx.teacher.databinding.ActivityAnswerSkillCommentBinding
import com.lancoo.znbkxx.teacher.model.data.ClassicWrongQue
import com.lancoo.znbkxx.teacher.model.data.RecQuesID
import com.lancoo.znbkxx.teacher.ui.fragment.*
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import com.lancoo.znbkxx.teacher.viewmodel.HardSolutionViewModel

import kotlinx.android.synthetic.main.activity_answer_skill_comment.*
import kotlinx.android.synthetic.main.activity_answer_skill_comment.vp
import kotlinx.android.synthetic.main.fmt_my_comment.*

/**
 *@author Admin
 *@date 2021/2/25
 *
 */
class AnswerSkillCommentActivity :
        BaseVMActivity<HardSolutionViewModel, ActivityAnswerSkillCommentBinding>(),
        View.OnClickListener {
    private val typeName by lazy { intent.getStringExtra("typeName") }
    private val typeNo by lazy { intent.getStringExtra("typeNo") }
    private val genreID by lazy { intent.getStringExtra("genreID") }
    private val tabs = arrayOf("我的讲评", "技巧讲解", "典型错题", "拓展训练")
    private var flag = 0
    override fun setLayoutID(): Int {
        return R.layout.activity_answer_skill_comment
    }

    override fun initView() {
        loadingDialog.show()
        iv_save.setOnClickListener(this)
        iv_publish.setOnClickListener(this)
        iv_back.setOnClickListener(this)
        getQTypeComments()
    }

    private fun getQTypeComments() {
        viewModel.getQTypeComments(Ext.STU_ID, typeNo, genreID)
    }

    private fun initViewPager() {
        vp.adapter = object : FragmentStateAdapter(supportFragmentManager, lifecycle) {
            override fun getItemCount(): Int {
                return 4
            }

            override fun createFragment(position: Int): Fragment {
                return when (position) {
                    0 -> MyCommentFragment()
                    1 -> SkillAnalysisFragment().apply {
                        arguments = bundleOf(Pair("typeNo", typeNo), Pair("genreID", genreID))
                    }
                    2 -> ExampleWrongQuesFragment().apply {
                        arguments = bundleOf(Pair("typeName", typeName))
                    }
                    else -> {
                        ExpandTrainFragment().apply {
                            arguments = bundleOf(Pair("typeName", typeName))
                        }
                    }
                }
            }
        }
        TabLayoutMediator(tbl_comment, vp) { tab: TabLayout.Tab, i: Int ->
            tab.text = this.tabs[i]
        }.attach()
    }

    override fun providerVMClass(): Class<HardSolutionViewModel>? {
        return HardSolutionViewModel::class.java
    }

    private fun updateSkillComment(StuID: String, flag: Int) {
        loadingDialog.show()
        val fmt = supportFragmentManager.findFragmentByTag("f0") as MyCommentFragment
        if (viewModel.answerSkillComment.value!!.AnsweringSkills !is String) {
            val codeList = mutableListOf<String>()
            viewModel.skillList.value!!.forEach {
                codeList.add(it.KnowledgeCode)
            }
            viewModel.answerSkillComment.value!!.AnsweringSkills = codeList.joinToString("|")
        }
        viewModel.saveAnswerSkillSolution(fmt.edt_content.text.toString(),fmt.selectList, fmt.tempList, StuID, flag)
    }

    override fun startObserve() {
        viewModel.answerSkillComment.observe(this, Observer {
            loadingDialog.dismiss()
            it.GenreID = genreID
            it.TypeNo = typeNo
            initViewPager()
        })
        viewModel.resultInfo.observe(this, Observer {
            loadingDialog.dismiss()
            if (it.result == 1) {
                MyToast.showToast(if (flag == 0) "保存成功" else "发布成功")
            } else {
                MyToast.showToast(if (flag == 0) "保存失败" else "发布失败")
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 3) {
            if (requestCode == 200) {
                val classicWrongQue = data?.getParcelableArrayListExtra<ClassicWrongQue>(
                        "ClassicWrongQues")
                viewModel.answerSkillComment.value!!.ClassicWrongQues = classicWrongQue!!
                (supportFragmentManager.findFragmentByTag(
                        "f2") as ExampleWrongQuesFragment).adapter.setNewData(classicWrongQue)
            } else if (requestCode == 300) {
                val recQuesIDs = data?.getParcelableArrayListExtra<RecQuesID>("RecQuesID")
                viewModel.answerSkillComment.value!!.RecQuesID = recQuesIDs!!
                (supportFragmentManager.findFragmentByTag(
                        "f3") as ExpandTrainFragment).adapter.setNewData(recQuesIDs)
            }
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_save -> {
                flag = 0
                updateSkillComment(Ext.STU_ID, flag)
            }
            R.id.iv_publish -> {
                flag = 1
                updateSkillComment(Ext.STU_ID, flag)
            }
            R.id.iv_back -> finish()
        }
    }
}